package bavaria.hightech.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bavaria.hightech.banking.ECurrency;
import bavaria.hightech.banking.IBankClientView;
import bavaria.hightech.banking.Money;
import bavaria.hightech.exception.WrongValueException;


public class Gui {

	boolean debug = true;
	JFrame window;
	JButton accountvalue;
	JButton transfer;
	JButton statement;
	JButton backToMainMenu;
	JPanel actuallyPanel;
	JLabel sectionHead;
	JLabel dynamicErrorMessage;
	JTextField textField;
	JTextField textField2;
	JTextField textField3;
	JTextField textField4;
	IBankClientView bank;
	NumberFormat numberFormatter;
	
	public Gui(IBankClientView bank){
		this.bank = bank;
		numberFormatter = NumberFormat.getCurrencyInstance(bank.getBankLocale());
		window = new JFrame("AdFu Bank");
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		createWindow();
		window.setSize(600, 300);
		window.setLocation(300, 200);
		window.setVisible(true);
		dynamicErrorMessage = new JLabel();
	}
	
	private void createWindow(){
		JPanel buttonpanel = new JPanel(new GridLayout(4,1));
		accountvalue = new JButton("Kontostand");
		accountvalue.addActionListener(new AccountValueListener());
		transfer = new JButton("\u00DC"+"berweisung");
		transfer.addActionListener(new TransferListener());
		statement = new JButton("Kontoauszug");
		backToMainMenu = new JButton("Hauptmen"+"\u00FC");
		backToMainMenu.addActionListener(new BackToMainMenuListener());
		buttonpanel.add(backToMainMenu);
		buttonpanel.add(accountvalue);
		buttonpanel.add(transfer);
		buttonpanel.add(statement);
		window.add(BorderLayout.WEST, buttonpanel);
		actuallyPanel = new JPanel();
		actuallyPanel.setLayout(new BorderLayout());
		openMainMenu();
	}
	
	private void openMainMenu(){
		sectionHead = new JLabel ("Hauptmen"+"\u00FC");
		actuallyPanel.removeAll();
		actuallyPanel.add(BorderLayout.NORTH, sectionHead);
		actuallyPanel.add(BorderLayout.CENTER, new JLabel(new ImageIcon("img/BankLogo.png")));
		window.add(BorderLayout.CENTER, actuallyPanel);
	}
	
	private void openAccountValue(){
		actuallyPanel.removeAll();
		sectionHead.setText("Kontostandsabfrage");
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		JLabel label = new JLabel("Bitte Kontonummer eingeben: ");
		textField = new JTextField(6);
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new GetAccountValueListener());
		panel.add(label);
		panel.add(textField);
		panel.add(okButton);
		actuallyPanel.add(BorderLayout.NORTH, sectionHead);
		actuallyPanel.add(BorderLayout.CENTER , panel);
		actuallyPanel.updateUI();
	}
	
	private void openTransfer(){
		actuallyPanel.removeAll();
		sectionHead.setText("\u00DC"+"berweisungsformular");
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(5,2));
		JLabel label = new JLabel ("Bitte Ihre Kontonummer eingeben:	");
		textField = new JTextField(6);
		JLabel label2 = new JLabel ("Bitte Zielkontonummer eingeben:	");
		textField2 = new JTextField(6);
		JLabel label3 = new JLabel ("Bitte Betrag eingeben in Euro:	");
		textField3 = new JTextField(6);
		JLabel label4 = new JLabel("Verwendungszweck: ");
		textField4 = new JTextField(6);
		JButton okButton = new JButton ("Absenden");
		okButton.addActionListener(new TransferSuccessListener());
		panel.add(label);
		panel.add(textField);
		panel.add(label2);
		panel.add(textField2);
		panel.add(label3);
		panel.add(textField3);
		panel.add(label4);
		panel.add(textField4);
		panel.add(okButton);
		centerPanel.add(BorderLayout.NORTH, panel);
		actuallyPanel.add(BorderLayout.NORTH, sectionHead);
		actuallyPanel.add(BorderLayout.CENTER, centerPanel);
		actuallyPanel.updateUI();
	}
	
	private void TransferSuccessful(int accountNumberFrom){
		actuallyPanel.removeAll();
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4, 1));
		JLabel label = new JLabel("Die "+"\u00DC"+"berweisung ist erfolgreich " +
				"durchgef"+"\u00FC"+"hrt worden.");
		JLabel label2 = new JLabel ("Ihr Kontostand: ");
		long accountValue = bank.getAccountBalance(accountNumberFrom);
		JLabel label3 = new JLabel(numberFormatter.format(accountValue));
		JButton okButton = new JButton ("Hauptmen"+"\u00FC");
		okButton.addActionListener(new BackToMainMenuListener());
		panel.add(label);
		panel.add(label2);
		panel.add(label3);
		panel.add(okButton);
		actuallyPanel.add(BorderLayout.NORTH , panel);
		actuallyPanel.updateUI();
	}
	
	private void TransferFailed(){
		actuallyPanel.removeAll();
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4, 1));
		JButton okButton2 = new JButton("\u00DC"+"berweisung");
		okButton2.addActionListener(new TransferListener());
		JButton okButton = new JButton("Hauptmen"+"\u00FC");
		okButton.addActionListener(new BackToMainMenuListener());
		panel.add(dynamicErrorMessage);
		panel.add(okButton2);
		panel.add(okButton);
		actuallyPanel.add(BorderLayout.NORTH , panel);
		actuallyPanel.updateUI();
	}
	
	private void ActionFailed(){
		actuallyPanel.removeAll();
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2,1));
		JButton okButton = new JButton("Hauptmen"+"\u00FC");
		okButton.addActionListener(new BackToMainMenuListener());
		panel.add(dynamicErrorMessage);
		panel.add(okButton);
		actuallyPanel.add(BorderLayout.NORTH , panel);
		actuallyPanel.updateUI();
	}
	
	private class BackToMainMenuListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			openMainMenu();
			actuallyPanel.validate();
			if (debug)
				System.out.println("BackToMainMenuListener");
		}
	}
	
	private class AccountValueListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			openAccountValue();
			actuallyPanel.validate();
			if (debug)
				System.out.println("AccountValueListener");
		}
	}
	
	private class GetAccountValueListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			actuallyPanel.removeAll();
			JPanel panel = new JPanel();
			panel.setLayout(new FlowLayout());
			JLabel label = new JLabel ("Ihr Kontostand: ");
			String text = null;
			try{
				text = textField.getText();
			} catch (NullPointerException e1){
				dynamicErrorMessage.setText("Bitte f"+"\u00FC"+"llen Sie das Feld aus.");
				ActionFailed();
				return;
			}
			
			int accountNumber = 0;
			try {
				accountNumber = Integer.parseInt(text);
			} catch (NumberFormatException e1) {
//				e1.printStackTrace();
				dynamicErrorMessage.setText("Sie haben einen falschen Wert eingegeben.");
				ActionFailed();
				return;
			}
			
			long accountValue = bank.getAccountBalance(accountNumber);
			if (accountValue == Long.MAX_VALUE){
				dynamicErrorMessage.setText("Kontonummer nicht vorhanden");
				ActionFailed();
				return;
			}
			
			JLabel label2 = new JLabel(numberFormatter.format(accountValue));
			JButton okButton = new JButton("Zur"+"\u00FC"+"ck");
			okButton.addActionListener(new BackToMainMenuListener());
			panel.add(label);
			panel.add(label2);
			panel.add(okButton);
			actuallyPanel.add(BorderLayout.NORTH, sectionHead);
			actuallyPanel.add(BorderLayout.CENTER, panel);
			actuallyPanel.updateUI();
			if (debug)
				System.out.println("GetAccountValueListener");
		}
	}
	
	private class TransferListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			openTransfer();
			actuallyPanel.validate();
			if (debug)
				System.out.println("TransferListener");
		}
	}
	
	private class TransferSuccessListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if (debug)
				System.out.println("TransferSuccessListener");
			
			String text1 = null;
			String text2 = null;
			String text3 = null;
			String text4 = null;
			try{
				text1 = textField.getText();
				text2 = textField2.getText();
				text3 = textField3.getText();
				text4 = textField4.getText();
			} catch (NullPointerException e1){
				dynamicErrorMessage.setText("Bitte f"+"\u00FC"+"llen Sie alle Felder aus.");
				TransferFailed();
				return;
			}
			
			int accountNumberFrom = 0;
			int accountNumberTo = 0;
			long value = 0;
			try {
				accountNumberFrom = Integer.parseInt(text1);
				accountNumberTo = Integer.parseInt(text2);
				value = (long) Double.parseDouble(text3);
			} catch (NumberFormatException e1) {
//				e1.printStackTrace();
				dynamicErrorMessage.setText("Sie haben einen falschen Wert eingegeben.");
				TransferFailed();
				return;
			}
			
			Money money = new Money(value, ECurrency.EUR);
			try {
				if (bank.transfer(accountNumberFrom, accountNumberTo, money, text4)){
					TransferSuccessful(accountNumberFrom);
					return;
				}
				else{
					dynamicErrorMessage.setText("Die "+"\u00DC"+"berweisung ist fehlgeschlagen.");
					TransferFailed();
					return;
				}
			} catch (WrongValueException e1) {
//				e1.printStackTrace();
				dynamicErrorMessage.setText("Fehler: Falscher Betrag");
				TransferFailed();
			}
		}
	}
}
